/*
 * (c) Copyright 2009 Hewlett-Packard Development Company, LP
 * All rights reserved.
 * [See end of file]
 */

package com.hp.hpl.jena.clusteredtdb;

import java.io.FileInputStream;
import java.util.Properties;
import com.hp.hpl.jena.clusteredtdb.pgraph.NodeMask;

public class ClusteredTDBConfig {
	
	int IDSize = 8;
	int tupleIds = 3;
	String dataFileBase;
	NodeMask mask;
	 
	VNodeConfigEntry[] vns;
	ServerConfigEntry[] svs;
	int vnsAdded = 0; 
	int svsAdded = 0;
	int defaultPort;
	
	int serverId = 0;
	
	
	
	int maxDNConns = 10;
	
	//Designed to be a singleton object describing the layout of the
	//network
	public ClusteredTDBConfig(String conf) {
		
		Properties configFile = new Properties();
		try {
			//configFile.load(this.getClass().getClassLoader().getResourceAsStream(conf));
			configFile.load(new FileInputStream(conf));
		} catch (Exception e) {
			System.err.println("Unable to find config!");
			System.err.println(e);
			System.exit(-1);
		}
		
		initServers(configFile.getProperty("SERVERS"), configFile);
		initVNodes(configFile.getProperty("VNODES"), configFile);
		this.serverId = Integer.parseInt(configFile.getProperty("SERVERID"));
		this.dataFileBase = configFile.getProperty("FILEBASE");
		
		this.mask = new NodeMask(12);
		
		this.defaultPort = 3030;
		
		this.IDSize = 8;
		this.tupleIds = 3;
	}

	private void initServers(String servers, Properties configFile) {
	    servers = servers.trim();
		String[] serverIds = servers.split(",");
		svs = new ServerConfigEntry[serverIds.length];
		for(int i = 0; i < serverIds.length; i++) {
			String[] server = split(configFile.getProperty("SERVER-"+serverIds[i]),",");
			this.svs[svsAdded] = new ServerConfigEntry(Integer.parseInt(serverIds[i]),server[0],Integer.parseInt(server[1]),Integer.parseInt(server[2]));
			svsAdded++;
		}
	}
	
	private void initVNodes(String vnodes, Properties configFile) {
	    vnodes = vnodes.trim() ;
		String[] vnodeIds = vnodes.split(",");
		vns = new VNodeConfigEntry[vnodeIds.length];
		for(int i = 0; i < vnodeIds.length; i++) {
			String[] vnode = split(configFile.getProperty("VNODE-"+vnodeIds[i]),",");
			this.vns[vnsAdded] = new VNodeConfigEntry(Integer.parseInt(vnodeIds[i]),Integer.parseInt(vnode[0]));
			vnsAdded++;
		}
	}

	static String [] split(String str, String sep)
	{
	    String[] x = str.split(sep) ;
	    if ( x != null )
	        return x ;
	    return new String[]{str} ;
	}
	
	public int getPort() {
		return defaultPort;
	}

	public int getIDSize() {
		return IDSize;
	}

	public int getIdsPerTuple() {
		return tupleIds;
	}

	public NodeMask getNodeMask() {
		return mask;
	}

	public int getServerId() {
		return serverId;
	}

	public int getMaxDNConns() {
		return maxDNConns;
	}

	public String getDataFileBase() {
		return dataFileBase;
	}

	public void setDataFileBase(String dataFileBase) {
		this.dataFileBase = dataFileBase;
	}

	public ServerConfigEntry[] getServers() {
		return svs;
	}

	public VNodeConfigEntry[] getVNodes() {
		return vns;
	}
	
	
}

/*
 * (c) Copyright 2009 Hewlett-Packard Development Company, LP
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
